#ifndef SOLUTION_H_
#define SOLUTION_H_
#ifndef NULL
#define NULL 0
#endif
struct ListNode
{

	int val;
	ListNode *next;
	ListNode(int x) : val(x), next(NULL) {}
};

class Solution
{

public:
	ListNode *deleteDuplicates(ListNode *head)
	{
		// Start typing your C/C++ solution below
		// DO NOT write int main() function
		if(!head)
			return NULL;

		ListNode *last = NULL, *current = head;
		while(current)
		{
			if(!(current->next) || (current->val != current->next->val))
			{
				if(!last)
					last = head;
				else
					last = last->next;

				last->val = current->val;
			}
			else
			{
				while(current->next && (current->val == current->next->val))
					current = current->next;
			}
			current = current->next;
		}

		if(last)
			last->next = NULL;
		else
			head = NULL;
		return head;
	}
};
#endif